import logging
from logging import handlers, Logger
import os


def getLogger(name: str='root') -> Logger:
    """
    获取日志器对象
    :param name: 日期器名字，默认为root
    :return: 日志器对象
    """

    logger: Logger = logging.getLogger(name)

    logger.setLevel(logging.DEBUG)

    if not logger.handlers:

        th: logging.StreamHandler = logging.StreamHandler()
        try:
            os.mkdir("logs")
        except:
            pass
        rf: handlers.RotatingFileHandler = handlers.RotatingFileHandler(
            filename=f"logs/{name}.log",
            mode='a',
            maxBytes=300*1024*1024,
            backupCount=10,
            encoding='utf-8'
        )

        th.setLevel(logging.INFO)
        rf.setLevel(logging.INFO)

        simple_formatter: logging.Formatter = logging.Formatter(
            fmt="{levelname} {asctime} {pathname}:{lineno} {message}",
            style="{"
        )
        verbose_formatter: logging.Formatter = logging.Formatter(
            fmt="【{name}】{levelname} {asctime} {pathname}:{lineno} {message}",
            datefmt="%Y-%m-%d %H:%M:%S",
            style="{"
        )

        th.setFormatter(simple_formatter)
        rf.setFormatter(verbose_formatter)


        logger.addHandler(th)
        logger.addHandler(rf)

    return logger


if __name__ == '__main__':

    logger = getLogger('dl')
    logger.info("这里是常规运行日志")
    logger.debug("开发人员在调试程序时自己手动打印的日志")
    logger.warning("这里是程序遇到未来会废弃的函数/方法时，输出的警告日志")
    logger.error("这里是程序发生错误时输出的日志")
    logger.critical("这是致命级别的日志，需要紧急修复的")


    logger1 = getLogger('dl')
    print(id(logger1), id(logger))